package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xiangyi.teachingsystem.entity.dto.CourseUserDto;
import com.xiangyi.teachingsystem.entity.po.Course;
import com.xiangyi.teachingsystem.entity.po.CourseUser;
import com.xiangyi.teachingsystem.entity.po.User;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.CourseService;
import com.xiangyi.teachingsystem.service.CourseUserService;
import com.xiangyi.teachingsystem.service.UserGroupService;
import com.xiangyi.teachingsystem.service.UserService;
import com.xiangyi.teachingsystem.validation.groups.CourseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
public class CourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    UserService userService;

    @PostMapping("/addTeachingCourse")
    public Result addCourse(@Validated @RequestBody Course course){
        User byId = userService.getById(course.getUserId());
        if (byId==null){
            return Result.failure("用户不存在");
        }
        Course courseInfo = courseService.getOneByCourseIdAndUserId(course.getCourseName(),course.getUserId());
        if (courseInfo!=null)
            return Result.failure("课程名称已存在");
        try {
            courseService.addCourse(course);
            return Result.success("课程添加成功").setData(course);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }

    @PostMapping("/teachingCourseList")
    public Result teachingCourseList(@RequestParam Integer userId){
        User byId = userService.getById(userId);
        if (byId==null)
            return Result.failure("用户不存在");
        try {
            List<Course> courses = courseService.teachingCourseList(userId);
            return Result.success("授课列表获取成功").setData(courses);
        }catch (Exception e){
            return Result.failure("服务器错误");
        }
    }

    @PostMapping("/updateTeachingCourse")
    public Result updateTeachingCourse(@Validated(CourseInfo.class) @RequestBody Course course){
        Course courseInfo = courseService.getById(course.getId());
        if (!Objects.equals(courseInfo.getCourseName(), course.getCourseName())){
            Course course_name = courseService.getOne(new QueryWrapper<Course>().eq("course_name", course.getCourseName()));
            if (course_name!=null)
                return Result.failure("课程名称已存在");
        }

        if (course.getUserId()!=null){
            User byId = userService.getById(course.getUserId());
            if (byId==null)
                return Result.failure("组长不存在，请检查userId");
        }

        try {
            courseService.updateById(course);
            return Result.success("授课信息更新成功");
        }catch (Exception e){
            return Result.failure("服务器错误");
        }
    }

    @GetMapping("/delCourse")
    public Result delCourse(@RequestParam Integer courseId){
        try {
            courseService.delCourse(courseId);
            return Result.success("课程删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }

    @GetMapping("/course/getById")
    public Result getById(@RequestParam Integer courseId){
        CourseUserDto course=courseService.getInfoById(courseId);
        return Result.success("课程信息查询成功").setData(course);
    }

    @GetMapping("/courseList")
    public Result courseList(){
        List<Course> courseList = courseService.courseList();
        return Result.success("课程列表查询成功").setData(courseList);
    }

}
